import { loginIn, loginOut } from '../../api/user'
import { setSession, getSession } from '../../libs/cache'

export default {
    state: {
        userName: '',
        userId: '',
        avatorImgPath: '',
        token: getSession('token')
    },
    mutations: {
        setUserName (state, name) {
            state.userName = name
        },
        setUserId (state, id) {
            state.userId = id
        },
        setUserImg (state, avatorPath) {
            state.avatorImgPath = avatorPath
        },
        setUserToken (state, token) {
            state.token = token
            setSession('token',token)
        }
    },
    actions: {
        // 登录
        userLoginIn ({ commit }, param) {
            return new Promise((resolve, reject) => {
                loginIn(param).then(res => {
					if(res.token_type && res.access_token){
                   		commit('setUserToken',res.token_type + " " + res.access_token)
                    		resolve(res)
                    }
                }).catch(err => {
                    resolve(err)
                })
            })
        },
        // 退出登录
        userLogOut ({ state, commit }, param) {
            return new Promise((resolve, reject) => {
                loginOut(param).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }
}
